<?php
/**
 * mysql数据库操作类
 * @author Administrator
 *
 */
class mysql{
    private $host = 'localhost';
    private $user = 'root';
    private $password = '';
    private $conn = '';
    protected $table = '';
    protected $feild = '*';
    protected $where = '';
    protected $group = '';
    protected $having = '';
    protected $order = '';
    protected $limit = '';
    protected $set = array();
    protected $values = array();
    /**
     * 构造方法：自动连接
     * @param string $db_name
     * @param string $host
     * @param string $user
     * @param string $password
     */
    public function __construct($db_name, $host = null, $user = null, $password = null){
        if(empty($db_name)){
            return false;
        }
        if(!empty($host)){
            $this->host = $host;
        }
        if(!empty($user)){
            $this->user = $user;
        }
        if(!empty($password)){
            $this->password = $password;
        }
        $mysql_resource = mysql_connect($this->host, $this->user, $this->password);
        if($mysql_resource){
            $this->conn = $mysql_resource;
        }else{
            return false;
        }
        mysql_select_db($db_name);
        mysql_query('set names utf8');
    }
    /**
     * sql执行
     * @param string $sql
     * @return boolean|multitype:multitype:
     */
    public function query($sql){
        if(empty($sql)){
            return false;
        }
        if(preg_match('/^select/i',$sql)){
            $re = mysql_query($sql, $this->conn);
            $arr =array();
            while ($content = mysql_fetch_assoc($re)){
                $arr[] = $content;
            }
            mysql_free_result($re);
            return $arr;
        }
        if(preg_match("/^insert/i", $sql)){
            $re =  mysql_query($sql, $this->conn);
            if($re){
                return mysql_insert_id($this->conn);
            }else{
                return false;
            }
        }
        if(preg_match('/^update|delete/i', $sql)){
            $re = mysql_query($sql, $this->conn);
            if($re){
                return mysql_affected_rows($this->conn);
            }else{
                return false;
            }
        }
    }
    
    
    
    public function table($table){
        $this->table = $table;
        return $this;
    }
    public function feild($feild){
        $this->feild = $feild;
        return $this;
    }
    public function where($where){
        $this->where = $where;
        return $this;
    }
    public function __call($function_name, $arr){//limit array('5')
        $this->$function_name = $arr[0];
        return $this;
    }
    public function set($arr){
        $this->set = $arr;
        return $this;
    }
    public function values($arr){
        $this->values = $arr;
        return $this;
    }
    public function select($debug = false){
        if(empty($this->table)){
            return false;
        }
        if(empty($this->feild)){
            $this->feild = '*';
        }
        if(!empty($this->where)){
            $where = ' WHERE '.$this->where;
        }else{
            $where = '';
        }
        if(!empty($this->group)){
            $group = ' GROUP BY '.$this->group;
        }else{
            $group = '';
        }
        if(!empty($this->having)){
            $having = ' HAVING '.$this->having;
        }else{
            $having = '';
        }
        if(!empty($this->order)){
            $order = 'ORDER BY '.$this->order;
        }else{
            $order = '';
        }
        if(!empty($this->limit)){
            $limit = ' LIMIT '.$this->limit;
        }else{
            $limit = '';
        }
        $sql = "SELECT {$this->feild} FROM {$this->table} {$where} {$group} {$having} {$order} {$limit}";
        $this->init();//重置数据
        if($debug){
            var_dump($sql);exit();
        }else{
            $re = $this->query($sql);
            return $re;
        }
    }
    public function update($arr = array()){//array('name'=>'大中国','type'=>2)
        if(empty($this->table)){
            return false;
        }
        if(empty($arr)){
            $arr = $this->set;
        }
        $temp = array();
        foreach ($arr as $key=>$row){
            if(is_numeric($row)){
                $temp[] = "{$key}={$row}";
            }else{
                $temp[] = "{$key}='{$row}'";
            }
        }
        $str = implode(',', $temp);
        if(!empty($this->where)){
            $where = ' WHERE '.$this->where;
        }else{
            $where = '';
        }
        $sql = "UPDATE {$this->table} SET {$str} {$where}";//"update book set name='大中国',type=2 where id=5"
        $this->init();
        $re = $this->query($sql);
        return $re;
    }
    public function delete(){
        if(empty($this->table)){
            return false;
        }
        if(!empty($this->where)){
            $where = ' WHERE '.$this->where;
        }else{
            $where = '';
        }
        $sql = "DELETE FROM {$this->table} {$where}";
        $this->init();
        $re = $this->query($sql);
        return $re;
    }
    public function insert($arr = array()){//array('name'=>'大中国','type'=>2)
        if(empty($this->table)){
            return false;
        }
        if(empty($arr)){
            $arr = $this->values;
        }
        $arr_keys = array_keys($arr);
        $arr_value = array_values($arr);
        $feild = implode(',', $arr_keys);
        $temp = array();
        foreach ($arr_value as $row){
            if(is_numeric($row)){
                $temp[] = $row;
            }else{
                $temp[] = "'{$row}'";
            }
        }
        $value = implode(',', $temp);
        $sql = "INSERT {$this->table}({$feild}) VALUES({$value})";
        $this->init();
        $re = $this->query($sql);
        return $re;
    }
    public function init(){
        $this->table = '';
        $this->feild = '*';
        $this->where = '';
        $this->group = '';
        $this->having = '';
        $this->order = '';
        $this->limit = '';
    }
    /**
     * 关闭连接
     */
    public function __destruct(){
        mysql_close($this->conn);
    }
}